import express, { Request, Response } from 'express';

import { ControllerInterface } from '../interfaces/controller.interface';
import { AuthService } from '../services/auth.service';

const user = {
  id: '001002003'
};

class LoginController implements ControllerInterface {
  public path;
  public router = express.Router();

  constructor({ path }) {
    this.path = path;
    this.initRoutes();
  }

  private initRoutes () {
    const p = this.path;
    this.router.get(`${p}`, this.homePage);
    this.router.get(`${p}/info`, this.login);
    this.router.get(`${p}/userId`, this.getUserId);
  }

  private homePage (req: Request, res: Response) {
    return res.render('index');
  }

  private login (req: Request, res: Response) {
    const authService = new AuthService();
    const jwtToken = authService.createToken(user);
    return res.json(jwtToken);
  }

  private getUserId (req: Request, res: Response) {
    const token = req.query.token;
    const authService = new AuthService();
    const tokenInfo: any = authService.verifyToken(token);
    return res.json({ id: tokenInfo.id });
  }

}

export default LoginController;
